<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/16
 * Time: 15:51
 */

namespace app\admin\controller;


use app\common\controller\ReturnCode;
use think\Controller;
use think\Request;
use app\admin\model\Order as OM;
use app\admin\model\Goods as G;
class Order extends Controller
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this -> aid = $request -> param('aid');
        $this -> token = $request ->param('token');
        $this -> gid = $request -> param('gid');
        $this -> oid = $request -> param('oid');
        $this -> spec = $request -> param('spec');

        $this -> hasToken = hasToken($this ->aid , $this ->token);
    }
    //列表
    public function index(){
        if(!isset($this -> aid) || empty($this -> token)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $res = OM::where('order_id','>',0) -> select();
            foreach ($res as $key => $val){

//                dump($val['order_spec']);
//                dump(json_decode($val['order_spec']));
                $arr = object_array(json_decode($val['order_spec']));
//                foreach ($arr as $k => $v){
//                    dump($v);
//                    //stdClass Object                                               -> 调用
////                    $v['g_name'] = G::where('goods_id','=',$v['goods_id'])->field('g_name')->find();
//                    $v['g_name'] = db('goods')->where('goods_id','=',$v['goods_id'])->field('g_name')->find();
//
////                    dump($v);
//                    foreach ($v['spec'] as $ke => $va){
//
////                        foreach ($va as $e => $a){
//////                            dump($e);
////                            if($e == 'feed'){
//////                                dump($a);
////                                if(strpos($a,'¥') !== false){
////                                    $price = trim(strrchr($a,'¥'),'¥');
//////                                    dump($a);
//////                                    $va[$e]['f_price'] = $price;
////                                }
////                            }
//                            $s = db('set_'.$ke)->where($ke.'_id','=',$va)->find();
////                            $va->$e = $s;
////                            $va[$e] = $s[$e.'_name'];
////                            $va[$e]['price'] = $price;
////                        }
////                        $v->spec = $va;
//                        $v['o_spec'][$ke] = $s[$ke.'_name'];
////                        dump($va);
//                    }
////                    dump($v);
//                    $arr[$k]  = $v;
//                }
//                print_r($arr);
                $res[$key]['order_spec'] = $arr;
            }
//            print_r($res);
//            die;
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0],'data' => $res]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }
    //添加
    public function orderadd(Request $request){
//        dump(file_get_contents('php://input'));
//        dump($GLOBALS);
//        dump(json_encode($request->param('spec')));                                                 //json
//        dump($request ->param('spec'));                                                             //from_data
//        die;
//        $order = $this -> order;
//        dump($order);
//        die;
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> spec)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }

        if($this -> hasToken){
            list($msec, $sec) = explode(' ', microtime());
            $data = [
                'o_id' => date('YmdHis',time()).substr((float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000),4),
                'order_spec' => $this -> spec,
                ];

            $res = OM::create($data)->allowField(true);
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }
    //详情
    public function orderinfo(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> oid)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $where = ['order_id' => $this -> oid];
            $res = OM::where($where)->find();
//            $res[]
            $arr = object_array(json_decode($res['order_spec']));
//            dump($arr);
            foreach ($arr as $key => $val){
                $name = db('goods')->where('goods_id','=',$val['goods_id'])->field('g_name')->find();
                $val['g_name'] = $name['g_name'];
                foreach ($val['spec'] as $k => $v){
//                    dump($v);
                    foreach ($v as $ke => $va){
//                        dump($va);
                        $os = db('set_'.$ke)->where($ke.'_id','=',$va)->find();
                        $v[$ke] = $os[$ke.'_name'];
                    }
//                    dump($v);
                    $val['og_spec'][$k] =$v;
                }
//                dump($val);
                $arr[$key] = $val;
            }
//            die;
            $res['o_spec'] = $arr;
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0],'data' => $res]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }
    //修改
    public function orderup(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> oid) || empty($this -> gid)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            list($msec, $sec) = explode(' ', microtime());
            $where = ['order_id' => $this -> oid];
            $res = OM::update([
                'goods_id' => $this -> gid,
                'o_id' => date('YmdHis',time()).substr((float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000),4),
                ],$where);
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }
    //删除
    public function orderdel(){
        if(!isset($this -> aid) || empty($this -> token) || empty($this -> oid)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this -> hasToken){
            $where = ['order_id' => $this -> oid];
            $has = OM::where($where)->find();
            if(!$has){
                return json(['status' => 23,'msg' => ReturnCode::$return_code[23]]);
            }
            $res = OM::destroy($where);
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg' => ReturnCode::$return_code[1]]);
        }
        return json(['status' => 3,'msg' => ReturnCode::$return_code[3]]);
    }
}